package com.neuedu.yyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.config.CommonResult;
import com.neuedu.yyzx.entity.Food;
import com.neuedu.yyzx.service.IFoodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 第7组....
 * @since 2021-04-29
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    IFoodService foodService;

    @RequestMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNo , @RequestParam(defaultValue = "10") Integer pageSize, Food food){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(food!=null && StringUtils.isNotEmpty(food.getName())){
            queryWrapper.like("name",food.getName());
        }

        return CommonResult.success(foodService.page(new Page<>(pageNo,pageSize),queryWrapper));

    }

    @RequestMapping("/add")
    CommonResult add(Food food){
        return CommonResult.success(foodService.saveOrUpdate(food));
    }

    @RequestMapping("/update")
    CommonResult update(Food food){
        return CommonResult.success(foodService.updateById(food));
    }
    @RequestMapping("/getById")
    CommonResult getById(Integer id){
        return CommonResult.success(foodService.getById(id));
    }

    @RequestMapping("/del")
    CommonResult del(Integer id){
        return CommonResult.success(foodService.removeById(id));
    }



}
